package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.base.model.BaseEntity;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author XIN
 * 2023/4/21
 */
@Service
@Slf4j
public class SkuManageServiceImpl implements SkuManageService {
	
	@Autowired
	private SkuInfoService skuInfoService;
	@Autowired
	private SkuSaleAttrValueService skuSaleAttrValueService;
	@Autowired
	private SkuAttrValueService skuAttrValueService;
	@Autowired
	private SkuImageService skuImageService;
	@Autowired
	private BaseAttrInfoService baseAttrInfoService;
	
	@Autowired
	private RedissonClient redissonClient;
	
	/**
	 * 保存商品SKU信息
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveSkuInfo(SkuInfo skuInfo) {
		//提交sku信息
		System.out.println(skuInfo.getId());
		skuInfoService.save(skuInfo);
		RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
		bloomFilter.add(skuInfo.getId());
		System.out.println(skuInfo.getId());
		//保存图片连接
		List<SkuImage> skuImageList = skuInfo.getSkuImageList();
		if (!CollectionUtils.isEmpty(skuImageList)){
			skuImageList.stream().forEach(skuImage -> {
				skuImage.setSkuId(skuInfo.getId());
			});
			skuImageService.saveBatch(skuImageList);
		}
		//平台属性信息
		List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
		if (!CollectionUtils.isEmpty(skuAttrValueList)){
			skuAttrValueList.stream().forEach(s->{
				s.setSkuId(skuInfo.getId());
			});
			skuAttrValueService.saveBatch(skuAttrValueList);
		}
		//销售属性
		List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
		if (!CollectionUtils.isEmpty(skuSaleAttrValueList)){
			skuSaleAttrValueList.stream().forEach(skuSaleAttrValue -> {
				skuSaleAttrValue.setSkuId(skuInfo.getId());
				skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
			});
			skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);
			
		}
		
		
	}
	
	/**
	 * 根据分类ID分页查询商品SKU列表
	 * @param iPage
	 * @param category3Id
	 * @return
	 */
	@Override
	public IPage<SkuInfo> list(IPage<SkuInfo> iPage, Long category3Id) {
		LambdaQueryWrapper<SkuInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SkuInfo::getCategory3Id,category3Id );
		queryWrapper.orderByDesc(SkuInfo::getUpdateTime);
		iPage = skuInfoService.page(iPage, queryWrapper);
		return iPage;
	}
	/**
	 * 上架
	 * @param skuId
	 * @return
	 */
	@Override
	public void onSale(Long skuId) {
		SkuInfo skuInfo = new SkuInfo();
		skuInfo.setId(skuId);
		skuInfo.setIsSale(1);
		skuInfoService.updateById(skuInfo);
	}
	
	/**
	 * 下架
	 * @param skuId
	 */
	@Override
	public void concelSale(Long skuId) {
		SkuInfo skuInfo = new SkuInfo();
		skuInfo.setId(skuId);
		skuInfo.setIsSale(0);
		skuInfoService.updateById(skuInfo);
	}
	/**
	 * 根据SKUID查询商品最新价格
	 * @param skuId
	 * @return
	 */
	@Override
	public BigDecimal getSkuPrice(Long skuId) {
		LambdaQueryWrapper<SkuInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseEntity::getId, skuId);
		queryWrapper.select(SkuInfo::getPrice);
		SkuInfo one = skuInfoService.getOne(queryWrapper);
		if (one!=null){
			return one.getPrice();
		}
		return new BigDecimal("0.0");
	}
	
	/**
	 * 根据SkuID查询当前商品包含平台属性以及属性值
	 * @param skuId
	 * @return
	 */
	@Override
	public List<BaseAttrInfo> getAttrList(Long skuId) {
		BaseAttrInfoMapper baseMapper = (BaseAttrInfoMapper) baseAttrInfoService.getBaseMapper();
		List<BaseAttrInfo> list = baseMapper.getAttrListBySkuId(skuId);
		if (CollectionUtils.isEmpty(list)){
			list.stream().forEach((baseAttrInfo)->{
				List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
				if (CollectionUtils.isEmpty(attrValueList)){
					baseAttrInfo.setAttrValue(attrValueList.get(0).getValueName());
				}
			});
		}
		return list;
	}
}
















